package com.ktjy.goods.controller;


import com.ktjy.common.annotation.Log;
import com.ktjy.common.core.controller.BaseController;
import com.ktjy.common.core.domain.AjaxResult;
import com.ktjy.common.core.page.TableDataInfo;
import com.ktjy.common.enums.BusinessType;
import com.ktjy.common.utils.poi.ExcelUtil;
import com.ktjy.goods.domain.TGoodsInfo;
import com.ktjy.goods.service.ITGoodsAttrValuesService;
import com.ktjy.goods.service.ITGoodsInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author author
 * @since 2025-03-15
 */
@RestController
@RequestMapping("/goods/info")
public class TGoodsInfoController extends BaseController {
    @Autowired
    private ITGoodsInfoService itGoodsInfoService;

    @Autowired
    private ITGoodsAttrValuesService itGoodsAttrValuesService;

    @PreAuthorize("@ss.hasPermi('goods:info:list')")
    @GetMapping("/list")
    public TableDataInfo list(TGoodsInfo tGoodsInfo)
    {
        System.out.println("goodsInfoList()--------------------");
        startPage();
        List<TGoodsInfo> list = itGoodsInfoService.selectGoodsList(tGoodsInfo);
        return getDataTable(list);
    }

    /**
     * 获取商品详细信息
     */
    @PreAuthorize("@ss.hasPermi('goods:info:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(itGoodsInfoService.getGoodsInfoById(id));
    }

    /**
     * 新增商品信息
     */
    @PreAuthorize("@ss.hasPermi('goods:info:add')")
    @Log(title = "商品信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody TGoodsInfo tGoodsInfo)
    {
        System.out.println("add--------------------------------");
        return toAjax(itGoodsInfoService.save(tGoodsInfo));
    }

    /**
     * 修改商品信息
     */
    @PreAuthorize("@ss.hasPermi('goods:info:edit')")
    @Log(title = "商品信息", businessType = BusinessType.UPDATE)
    @PutMapping("/edit")
    public AjaxResult edit(@RequestBody TGoodsInfo tGoodsInfo)
    {
        return toAjax(itGoodsInfoService.updateById(tGoodsInfo));
    }

    @PreAuthorize("@ss.hasPermi('goods:info:remove')")
    @Log(title = "规格属性", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable List<Integer> ids)
    {
        System.out.println("remove--------------------------------");
        return toAjax(itGoodsInfoService.deleteGoodsAndAttrValues(ids));
    }


    /**
     * 导出规格属性列表
     */
//    @PreAuthorize("@ss.hasPermi('goods:info:export')")
//    @Log(title = "商品信息导出", businessType = BusinessType.EXPORT)
//    @PostMapping("/export")
//    public void export(HttpServletResponse response, TGoodsInfo tGoodsInfo)
//    {
//        List<TGoodsInfo> list = itGoodsInfoService.selectGoodsList(null);
//        ExcelUtil<TGoodsInfo> util = new ExcelUtil<TGoodsInfo>(TGoodsInfo.class);
//        util.exportExcel(response, list, "商品信息数据");
//    }

    @PostMapping("/export")
    public void exportExcel(TGoodsInfo tGoodsInfo, HttpServletResponse response) throws IOException {
//        ResponseEntity<byte[]>
        // 创建示例数据
        List<TGoodsInfo> list = itGoodsInfoService.selectGoodsList(tGoodsInfo);

        ExcelUtil<TGoodsInfo> util = new ExcelUtil<TGoodsInfo>(TGoodsInfo.class);
        util.exportExcel(response, list, "规格属性数据");


    }

//    private static final String ROOT_PATH=System.getProperty("user.dir")+ File.separator+"files";
//
//    @PostMapping("/upload")
//    public ResultUtil<?> upload(MultipartFile file) {
//        System.out.println("文件上传.............file="+file);
//        System.out.println("文件上传路径.............ROOT_PATH="+ ROOT_PATH);
//
//        String originalFilename = file.getOriginalFilename();   //获取文件的原始名称
//        System.out.println("originalFilename="+originalFilename);
//
//
//        try {
//            //构建路径：E:\idea_workSpace\360\vuespringboot\files\aaa.png
//            File saveFile = new File(ROOT_PATH+File.separator+originalFilename);
//            file.transferTo(saveFile);     //存储文件到本地地磁盘
//        } catch (IOException e) {
//            e.printStackTrace();
//            return ResultUtil.error("文件上传失败~");
//        }
//
//        return ResultUtil.success("文件上传成功~");
//    }
}
